package com.qxbb.report.utils;

import org.springframework.util.StringUtils;

/**
 * @Description: $
 * @Author: dyq
 * @Date: $
 */
public class HideKeyUtils {

//    public static void main(String[] args) {
//        String phone = "17203947561";
//        String idcard = "450303200101056845";
//        String name = "张三丰";
//
//        String p = mobileEncrypt(phone);
//        String id = idEncrypt(idcard);
//        String n = nameDesensitization(name);
//
//        System.out.println("我是脱敏手机号：" + p);
//        System.out.println("我是脱敏身份证：" + id);
//        System.out.println("我是脱敏名称：" + n);
//    }

    /**
     * 手机号码前三后四脱敏
     *
     * @param phone
     * @return
     */
    public static String mobileEncrypt(String phone) {
        if (StringUtils.isEmpty(phone) || (phone.length() != 11)) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 身份证前三后四脱敏
     *
     * @param idcard
     * @return
     */
    public static String idEncrypt(String idcard) {
        if (StringUtils.isEmpty(idcard) || (idcard.length() < 8)) {
            return idcard;
        }
        return idcard.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
    }

    /**
     * 名字脱敏
     * 规则，张三丰，脱敏为：张*丰
     *
     * @param name
     * @return
     */
    public static String nameDesensitization(String name) {
        if (name == null || name.isEmpty()) {
            return "";
        }
        String myName = null;
        char[] chars = name.toCharArray();
        if (chars.length == 1) {
            myName = name;
        }
        if (chars.length == 2) {
            myName = name.replaceFirst(name.substring(1), "*");
        }
        if (chars.length > 2) {
            myName = name.replaceAll(name.substring(1, chars.length - 1), "*");
        }
        return myName;
    }
}
